﻿Imports System.Collections.Generic

Partial Class WebForm_ImportVolunteer_SearchVolunteer
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(Me.btnExport)

        If Not IsPostBack Then
            InitDataAndShow()
        End If

        '若對此表單有管理權限，則可看到局列管名單  2010/04/20  Written By jibba
        If clsAuth.IsAdministrator Then
            btnBlackQuery.Visible = True
        Else
            btnBlackQuery.Visible = False
        End If

    End Sub

    '查詢
    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
 
        Me.BindGV()
        Me.GridView_Volunteers.Columns(9).Visible = False


    End Sub

    Public Event SelectedIndexChanged(ByVal source As Object, ByVal e As EventArgs)

    Protected Sub GridView_Volunteers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView_Volunteers.SelectedIndexChanged
        RaiseEvent SelectedIndexChanged(Me, New EventArgs)
    End Sub

    Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
        '        Me.Export()
    End Sub


    Protected Sub btnBlackQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBlackQuery.Click
    
        Me.BindBlackGV()
        Me.GridView_Volunteers.Columns(9).Visible = True

    End Sub

    Protected Sub GridView_Volunteers_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView_Volunteers.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If CType(e.Row.FindControl("lbBlackList"), Label).Text.Trim().Length > 5 Then
                CType(e.Row.FindControl("lbBlackList"), Label).Text = CType(e.Row.FindControl("lbBlackList"), Label).Text.Substring(0, 5) + "..."
            End If
        End If

    End Sub

    Protected Sub GridView_Volunteers_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView_Volunteers.PageIndexChanging
        Me.GridView_Volunteers.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub GridView_Volunteers_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView_Volunteers.Sorting
        Me.BindGV()
    End Sub
End Class

Partial Class WebForm_ImportVolunteer_SearchVolunteer

    Public Sub InitDataAndShow()

        VolunteerQueryControl1.InitData()
        ' RaiseEvent Loaded(Me, New EventArgs)
    End Sub

    Public Sub BindGV()
        'ObjectDataSource1.SelectParameters.Item("Region").DefaultValue = VolunteerQueryControl1.Region
        'ObjectDataSource1.SelectParameters.Item("Region2").DefaultValue = VolunteerQueryControl1.Region2
        'ObjectDataSource1.SelectParameters.Item("VOrganizationID").DefaultValue = VolunteerQueryControl1.VOrganizationID
        'ObjectDataSource1.SelectParameters.Item("VolunteerId").DefaultValue = VolunteerQueryControl1.VolunteerId
        'ObjectDataSource1.SelectParameters.Item("VolunteerName").DefaultValue = VolunteerQueryControl1.VolunteerName
        'ObjectDataSource1.SelectParameters.Item("TEL").DefaultValue = VolunteerQueryControl1.VolunteerTEL

        'GridView_Volunteers.DataSource = ObjectDataSource1
        'GridView_Volunteers.DataBind()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0

        sql.AppendFormat(" Select v.VolunteerID, v.VolunteerName, v.OfficePhone, v.BlackList, ")
        sql.AppendFormat(" vs.TrainingBHour , vs.TrainingSHour , vs.TrainingWHour , vs.WorkHour, vs.OpenWorkHour, vs.EffectiveWorkHour, oo.VOrganizationName,oo.VOrganizationID ")
        'sql.AppendFormat(" ,B.RegionName,oo.DirectUOrganizationID ")
        sql.AppendFormat(" From VolunteerDistinctView v inner join VolunteerStat vs ON v.VolunteerID = vs.VolunteerID ")
        'sql.AppendFormat(" LEFT JOIN  Village B on v.VillageId=B.RegionID ")

        'sql.AppendFormat(" left JOIN (select vm.VOrganizationID,vo.VOrganizationName,vm.VolunteerID,vo.DirectUOrganizationID from VOrganizationMapping vm ")
        sql.AppendFormat(" left JOIN (select vm.VolunteerID,vm.VOrganizationID,vo.VOrganizationName,vo.DirectUOrganizationID from VOrganizationMapping vm ")
        sql.AppendFormat("inner join VOrganizationViewV2 vo on vm.VOrganizationID=vo.VOrganizationID ) oo on v.VolunteerID=oo.VolunteerID  ")

        'sql.AppendFormat(" LEFT JOIN OrganizationView ov on vo.VOrganizationID=ov.VOrganizationID ")
        sql.AppendFormat("WHERE v.Active in ('1','2','Y') ")
        If VolunteerQueryControl1.OrganizationID <> "0" Then
            sql.AppendFormat("AND  oo.DirectUOrganizationID ='{0}'   ", VolunteerQueryControl1.OrganizationID)
        End If
        If VolunteerQueryControl1.VOrganizationID <> "0" Then
            sql.AppendFormat("AND oo.VOrganizationID={0}  ", VolunteerQueryControl1.VOrganizationID)
        End If
        'sql.AppendFormat("AND (v.VillageID IN (SELECT RegionID FROM Region WHERE Parent='{0}' or '{0}' ='' )) ", VolunteerQueryControl1.Region)
        'sql.AppendFormat("AND (v.VillageID ='{0}' or '{0}' ='' ) ", VolunteerQueryControl1.Region2)
        If VolunteerQueryControl1.VolunteerId.Trim() <> "" Then
            sql.AppendFormat("AND (v.VolunteerId = '{0}') ", VolunteerQueryControl1.VolunteerId)
        End If
        If VolunteerQueryControl1.VolunteerName.Trim() <> "" Then
            '      sql.AppendFormat("AND (v.VolunteerName like '%{0}%') ", VolunteerQueryControl1.VolunteerName)
            sql.AppendFormat("AND ltrim(replace(replace(v.VolunteerName, '  ', ''), ' ', '')) like '%{0}%' ", LTrim(VolunteerQueryControl1.VolunteerName.Replace("  ", "").Replace(" ", "")))
        End If
        If VolunteerQueryControl1.VolunteerTEL.Trim <> "" Then
            sql.AppendFormat("AND (((v.MobilePhone like '%{0}%') or (v.OfficePhone like '%{0}%'))) ", VolunteerQueryControl1.VolunteerTEL)
        End If

        If GridView_Volunteers.SortSQL <> "" Then
            sql.AppendFormat(" Order By {0} ", GridView_Volunteers.SortSQL)
        Else
            sql.AppendFormat(" Order By v.VolunteerID ")
        End If
        Dim dt As Data.DataTable
        'MyObj.WriteLog("sql:" & sql.ToString())
        dt = dat.GetData(sql.ToString)
        'MyObj.WriteLog("Row:" & dt.Rows.Count)
        GridView_Volunteers.DataSource = dt
        GridView_Volunteers.DataBind()

    End Sub

    Public Sub BindBlackGV()

        'ObjectDataSource1.SelectParameters.Item("Region").DefaultValue = VolunteerQueryControl1.Region
        'ObjectDataSource1.SelectParameters.Item("Region2").DefaultValue = VolunteerQueryControl1.Region2
        'ObjectDataSource1.SelectParameters.Item("UOrganizationID").DefaultValue = VolunteerQueryControl1.OrganizationID
        'ObjectDataSource1.SelectParameters.Item("VOrganizationID").DefaultValue = VolunteerQueryControl1.VOrganizationID
        'ObjectDataSource1.SelectParameters.Item("VolunteerId").DefaultValue = VolunteerQueryControl1.VolunteerId
        'ObjectDataSource1.SelectParameters.Item("VolunteerName").DefaultValue = VolunteerQueryControl1.VolunteerName
        'ObjectDataSource1.SelectParameters.Item("TEL").DefaultValue = VolunteerQueryControl1.VolunteerTEL

        'GridView_Volunteers.DataSource = ObjectDataSource2
        'GridView_Volunteers.DataBind()


        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0

        sql.AppendFormat(" Select v.VolunteerID, v.VolunteerName, B.RegionName, v.OfficePhone, v.BlackList, ")
        sql.AppendFormat(" vs.TrainingBHour , vs.TrainingSHour , vs.TrainingWHour , vs.WorkHour, vs.OpenWorkHour, vs.EffectiveWorkHour, oo.VOrganizationName,oo.DirectUOrganizationID,oo.VOrganizationID ")
        sql.AppendFormat(" From VolunteerDistinctView v inner join VolunteerStat vs ON v.VolunteerID = vs.VolunteerID ")
        sql.AppendFormat(" LEFT JOIN  Village B on v.VillageId=B.RegionID ")

        sql.AppendFormat("  left JOIN (select vm.VOrganizationID,vo.VOrganizationName,vm.VolunteerID,vo.DirectUOrganizationID from VOrganizationMapping vm ")
        sql.AppendFormat("inner join VOrganizationViewV2 vo on vm.VOrganizationID=vo.VOrganizationID ) oo on v.VolunteerID=oo.VolunteerID  ")

        'sql.AppendFormat(" LEFT JOIN OrganizationView ov on vo.VOrganizationID=ov.VOrganizationID ")
        sql.AppendFormat("WHERE v.Active in ('1','2','Y') ")
        If VolunteerQueryControl1.OrganizationID <> "0" Then
            sql.AppendFormat("AND  oo.DirectUOrganizationID ='{0}'   ", VolunteerQueryControl1.OrganizationID)
        End If
        If VolunteerQueryControl1.VOrganizationID <> "0" Then
            sql.AppendFormat("AND oo.VOrganizationID={0}  ", VolunteerQueryControl1.VOrganizationID)
        End If
        'sql.AppendFormat("AND (v.VillageID IN (SELECT RegionID FROM Region WHERE Parent='{0}' or '{0}' ='' )) ", VolunteerQueryControl1.Region)
        'sql.AppendFormat("AND (v.VillageID ='{0}' or '{0}' ='' ) ", VolunteerQueryControl1.Region2)
        sql.AppendFormat("AND (v.VolunteerId = '{0}' or '{0}'='') ", VolunteerQueryControl1.VolunteerId)
        sql.AppendFormat("AND (v.VolunteerName like '%{0}%' or '{0}'='') ", VolunteerQueryControl1.VolunteerName)
        sql.AppendFormat("AND (((v.MobilePhone like '%{0}%' or '{0}'='') or (v.OfficePhone like '%{0}%' or '{0}'=''))) ", VolunteerQueryControl1.VolunteerTEL)
        sql.AppendFormat(" And v.BlackList <> '' And v.BlackList is not null ")

        If GridView_Volunteers.SortSQL <> "" Then
            sql.AppendFormat(" Order By {0} ", GridView_Volunteers.SortSQL)
        Else
            sql.AppendFormat(" Order By v.VolunteerID ")
        End If
        GridView_Volunteers.DataSource = dat.GetData(sql.ToString)
        GridView_Volunteers.DataBind()
    End Sub

    Public ReadOnly Property SelectedValue() As String
        Get
            Return GridView_Volunteers.SelectedValue
        End Get
    End Property
    Public Property VOrganizationID() As String
        Get
            If Me.GridView_Volunteers.SelectedRow Is Nothing Then
                ViewState("VOrganizationID") = Me.GridView_Volunteers.DataKeys(1)("VOrganizationID").ToString()
            Else
                ViewState("VOrganizationID") = Me.GridView_Volunteers.DataKeys(Me.GridView_Volunteers.SelectedRow.RowIndex)("VOrganizationID").ToString()
            End If
            Return ViewState("VOrganizationID")
        End Get
        Set(ByVal value As String)
            ViewState("VOrganizationID") = value

        End Set
    End Property
    '  Public Event Loaded(ByVal source As Object, ByVal e As EventArgs)
End Class